package com.hss.common.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.pactera.cpicuat.BuildConfig;

/**
 * Created by Administrator on 2017/10/9.
 */

public class SetUtil {
    /**
     * 跳转到权限设置界面
     */
    public static  void startAppDetailSettingIntent(Context context){

       String brand =   SystemUtil.getDeviceBrand();
        if  (TextUtils.isEmpty(brand)){
            gotoPermissionDefault(context);
        }else if (brand.contains("miui")){
            gotoMiuiPermission(context);
        }else if (brand.contains("meizu")){
            gotoMeizuPermission(context);
        }else if (brand.contains("huawei")){
            gotoHuaweiPermission(context);
        }else {
            gotoPermissionDefault(context);
        }
    }


    /**
     * 跳转到魅族的权限管理系统
     */
   static private void gotoMeizuPermission(Context context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            gotoPermissionDefault(context);
        }
    }

    /**
     * 华为的权限管理页面
     */
    static  private void gotoHuaweiPermission(Context context) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            gotoPermissionDefault(context);
        }

    }

    /**
     * 跳转到miui的权限管理页面
     */
    static  private void gotoMiuiPermission(Context context) {
        Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
        ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
        i.setComponent(componentName);
        i.putExtra("extra_pkgname",context. getPackageName());
        try {
            context.startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
            gotoPermissionDefault(context);
        }
    }

    static private void  gotoPermissionDefault(Context context) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(Build.VERSION.SDK_INT >= 9){
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if(Build.VERSION.SDK_INT <= 8){
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(intent);
    }
}
